package com.augurit.wf.jbpmex.cmd;

import java.util.ArrayList;
import java.util.List;

import org.jbpm.api.cmd.Environment;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.cmd.AbstractCommand;
import org.jbpm.pvm.internal.history.model.HistoryActivityInstanceImpl;
import org.jbpm.pvm.internal.history.model.HistoryTaskImpl;
import org.jbpm.pvm.internal.model.ActivityImpl;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.model.TransitionImpl;
import org.jbpm.pvm.internal.session.DbSession;
import org.jbpm.pvm.internal.task.TaskImpl;

import com.augurit.wf.client.summary.service.IWfTaskSummaryService;
import com.augurit.wf.common.util.WfUtils;

public class TakeBackMultiTasksCmd extends AbstractCommand<Void>{

	private static final long serialVersionUID = -4993984682747433400L;
	
	private List<Task> list;
	private String hisTaskId;
	
	
	public TakeBackMultiTasksCmd(List<Task> list,String hisTaskId){
		this.list = list;
		this.hisTaskId = hisTaskId;
	}
	
	public Void execute(Environment environment) throws Exception {
		DbSession session = environment.get(DbSession.class);
		
		String taskId = WfUtils.getMainTaskId(list);
		TaskImpl mainTask = session.get(TaskImpl.class, Long.valueOf(taskId));
		HistoryTaskImpl hisTask = session.get(HistoryTaskImpl.class, Long.valueOf(hisTaskId));
		ExecutionImpl execution = mainTask.getExecution();
		ActivityImpl activity = (ActivityImpl)execution.getActivity();
		
		String destActivityName = hisTask.getActivityName();
		
		if(WfUtils.isAllTasksNotSigned(list)){
			//建立转向
			CreateTransitionCmd cmd =  new CreateTransitionCmd(execution.getId(),destActivityName);
			cmd.execute(environment);

			String newTrainsitionName = "to "+ destActivityName;
			
			//删除所有的task和hisTask对应的所有记录
			deleteHistory(hisTask,environment);
			
			//跳转
			execution.take(newTrainsitionName);
			
			//删除建立的转向
			deletTransition(activity,newTrainsitionName);
		}
		return null;
	}
	
	public void deletTransition(ActivityImpl activity,String trainsitionName ){
		if(activity != null){
			
			//获取新建立的转向
			TransitionImpl transition = (TransitionImpl)activity.findOutgoingTransition(
					trainsitionName);
			
			//删除建立的转向
			activity.removeOutgoingTransition(transition);
		}
	}
	
	
	public void deleteHistory(HistoryTaskImpl hisTask,Environment environment){
		if(environment != null){
			DbSession session = environment.get(DbSession.class);
			
			//要先查出来再能删除,删除一个再查询则查询结果会为空
			List<TaskImpl> taskList = this.getTasksWithDbSession(session);
			
			String mainTaskId = WfUtils.getMainTaskId(list);
			HistoryActivityInstanceImpl hisActInst = WfUtils.getHisActInst(environment,hisTask.getExecutionId(),mainTaskId );
			List<HistoryTaskImpl> hisTaskList = this.getHisTasksWithDbSession(session);
			
			HistoryActivityInstanceImpl hisHisActInst = WfUtils.getHisActInst(environment,hisTask.getExecutionId(),hisTaskId );
			
			//删除当前的task表对应的summary
//			deleteSumarryList(environment);
			
			//删除当前task列表以及对应的历史记录
			session.delete(hisActInst);
			deleteTasks(session,taskList);
			deleteHistoryTasks(session,hisTaskList);
			
			//删除需要取回的历史任务以及它对应的历史记录
			session.delete(hisHisActInst);
			session.delete(hisTask);
		}
	}
	
	//删除当前的task表对应的summary
//	public void deleteSumarryList(Environment environment){
//		if(environment != null){
//			IWfTaskSummaryService summaryService = environment.get(IWfTaskSummaryService.class);
//			
//			for(Task task: list){
//				TaskImpl taskImpl = (TaskImpl) task;
//				summaryService.deleteSummaryTask(taskImpl.getDbid());
//			}
//		}
//	}
	
	public void deleteTasks(DbSession session,List<TaskImpl> list){
		if(session != null && list != null && list.size()>0){
			for(TaskImpl task:list)
				session.delete(task);
		}
	}
	
	public void deleteHistoryTasks(DbSession session, List<HistoryTaskImpl> list){
		if(session != null && list != null && list.size()>0){
			for(HistoryTaskImpl task:list)
				session.delete(task);
		}
	}
	
	public List<TaskImpl> getTasksWithDbSession(DbSession session){
		List<TaskImpl> result =  null;
		if(session != null){
			result = new ArrayList<TaskImpl>();
			for(Task task:list){
				TaskImpl myTask = session.get(TaskImpl.class, Long.valueOf(task.getId()));
				if(myTask != null)
					result.add(myTask);
			}
		}
		return result;
	}
	
	public List<HistoryTaskImpl> getHisTasksWithDbSession(DbSession session){
		List<HistoryTaskImpl> result =  null;
		if(session != null){
			result = new ArrayList<HistoryTaskImpl>();
			for(Task task:list){
				HistoryTaskImpl myTask = session.get(HistoryTaskImpl.class, Long.valueOf(task.getId()));
				if(myTask != null)
					result.add(myTask);
			}
		}
		return result;
	}
	
}
